/**
 * Copyright 2016-2018 mobaas.com
 */
package com.mobaas.devops.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mobaas.devops.dao.ContainerDao;
import com.mobaas.devops.model.Container;
import com.mobaas.devops.model.ContainerStat;
import com.mobaas.devops.service.ContainerService;

/**
 * 
 * @author billy zhang
 * 
 */
@Service
public class ContainerServiceImpl implements ContainerService {

	@Autowired
	private ContainerDao containerDao;

	@Override
	public Container selectContainerById(String containerId) {
		
		return containerDao.selectContainerById(containerId);
	}

	@Override
	public Container selectContainerByHost(String host) {
		String[] strs = host.split(":");
		String hostIp = "";
		int port = 80;
		if (strs.length > 0) {
			hostIp = strs[0];
		}
		if (strs.length > 1) {
			port = Integer.parseInt(strs[1]);
		}
		return containerDao.selectContainerByHostAndPort(hostIp, port);
	}

	@Override
	public int updateContainer(Container cont) {
		return containerDao.updateContainer(cont);
	}

	@Override
	public void insertContainer(Container cont) {
		containerDao.insertContainer(cont);
		
	}
	
	@Override
	public void insertContainerStat(ContainerStat stat) {
		containerDao.insertContainerStat(stat);
	}

	@Override
	public List<ContainerStat> selectContainerStatTotalByApp(String appId, Date startDate, int interval) {
		
		return containerDao.selectContainerStatsTotalByApp(appId, startDate, interval);
	}

	@Override
	public List<ContainerStat> selectContainerStatTotalByHostIp(String hostIp, Date startDate) {
		
		return containerDao.selectContainerStatsTotalByHostIp(hostIp, startDate);
	}

	@Override
	public Map<String, Integer> selectLastContainerStatMap() {
		Map<String, Integer> map = new HashMap<>();
		List<Map> list = containerDao.selectLastContainerStatMap();
		for (Map m : list) {
			map.put((String)m.get("containerId"), (Integer)m.get("seqNo"));
		}
		
		return map;
	}
	
	
	
	
}
